package com.dabaozhan.modules.wx_app.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dabaozhan.common.exception.RRException;
import com.dabaozhan.common.utils.ValidationBeanUtils;
import com.dabaozhan.common.utils.aliyun.ocr.BankCardutils;
import com.dabaozhan.modules.wx_app.base.ResponseDto;
import com.dabaozhan.modules.wx_app.entity.AccountEntity;
import com.dabaozhan.modules.wx_app.entity.BankcardInfoEntity;
import com.dabaozhan.modules.wx_app.entity.SmsEntity;
import com.dabaozhan.modules.wx_app.enums.ResponseCodeEnum;
import com.dabaozhan.modules.wx_app.service.BankcardInfoService;
import com.dabaozhan.modules.wx_app.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author houzhong
 * @date 2018-12-29 15:25
 */

@Api(value = "打包站用户银行卡", description = "打包站用户接口")
@RestController
@RequestMapping("/wx_app")
public class BankcardInfoController {

    private static final Logger LOGGER = LoggerFactory.getLogger(BankcardInfoController.class);

    @Autowired
    private BankcardInfoService bankcardInfoService;

    /**短信*/
    @Autowired
    private SmsService smsService;

    @ApiOperation(value = "银行卡查询")
    @RequestMapping(value = "/bankUserId")
    public String bankUserId(String userId) {
        ResponseDto<BankcardInfoEntity> responseDto = new ResponseDto<BankcardInfoEntity>();
        try {
            BankcardInfoEntity v = bankcardInfoService.findByUserId(userId);
            if (v != null) {
                responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
                responseDto.setResult(v);
            }else {
                responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_400);
            }
        } catch(RRException re){
            responseDto.setCode(re.getCode());
            responseDto.setMessage(re.getMessage());
        } catch (Exception e) {
            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
            //记录异常日志
            LOGGER.error("银行卡查询发生异常:",e);
        } finally {
            //记录请求入参和返回日志
            LOGGER.info("银行卡查询发生异常:{}-返回结果:{}",JSON.toJSONString(userId),JSON.toJSONString(responseDto));
        }
        return JSON.toJSONString(responseDto);
    }

    @ApiOperation(value = "银行卡新增")
    @RequestMapping(value = "/saveBankCard")
    public String saveBankCard(BankcardInfoEntity v,String code) {
        ResponseDto<BankcardInfoEntity> responseDto = new ResponseDto<BankcardInfoEntity>();
        try {
            SmsEntity sms = new SmsEntity();
            sms.setPhone(v.getPhone());
            sms.setCode(code);
            sms.setStatus(5);
            List<SmsEntity> smsVo = smsService.findPhone(sms);
            if (smsVo.size() > 0) {
                if (smsVo.get(smsVo.size() - 1).getPhone().equals(v.getPhone())) {
                    if (code.equals(smsVo.get(smsVo.size() - 1).getCode())) {
                        BankcardInfoEntity bankcardInfo = bankcardInfoService.findByUserId(v.getUserId());
                        if (bankcardInfo == null) {
                            bankcardInfo = new BankcardInfoEntity();
                        }

                        Map<String, String> querys = new HashMap<String, String>();
                        //银行卡号
                        querys.put("accountNo", v.getBankCardNumber());
                        //身份证号
                        querys.put("idCard", v.getCardNumber());
                        //手机号
                        querys.put("mobile", v.getPhone());
                        //所属人名称
                        querys.put("name", v.getBankName());

                        //返回银行卡信息
                        JSONObject jsonObject =  BankCardutils.getBankCard(querys);
                        //是否成功状态
                        String status = jsonObject.getString("status");

                        if (status.equals("01")) {
                            //所属银行
                            String bankCardName = jsonObject.getString("bank");
                            //用户id
                            bankcardInfo.setUserId(v.getUserId());
                            //所属人
                            bankcardInfo.setBankName(v.getBankName());
                            //身份证号
                            bankcardInfo.setCardNumber(v.getCardNumber());
                            //电话号码
                            bankcardInfo.setPhone(v.getPhone());
                            //所属银行
                            bankcardInfo.setBankCardName(bankCardName);
                            //银行卡号
                            bankcardInfo.setBankCardNumber(v.getBankCardNumber());

                            if (bankcardInfo.getId() != null) {
                                bankcardInfoService.updateSiteBankcardInfo(bankcardInfo);
                            }else {
                                bankcardInfoService.saveSiteBankcardInfo(bankcardInfo);
                            }
                            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
                            responseDto.setResult(bankcardInfo);
                        }else {
                            //认证失败
                            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_401);
                        }
                    }else {
                        responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_201);
                    }
                }else {
                    responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_202);
                }
            }else {
                responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_203);
            }
        }catch(RRException re){
            responseDto.setCode(re.getCode());
            responseDto.setMessage(re.getMessage());
        } catch (Exception e) {
            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
            //记录异常日志
            LOGGER.error("银行卡新增发生异常:",e);
        } finally {
            //记录请求入参和返回日志
            LOGGER.info("银行卡新增发生异常:{}-返回结果:{}",JSON.toJSONString(v),JSON.toJSONString(responseDto));
        }

        return JSON.toJSONString(responseDto);
    }
}
